//
//  TFVoteSelectCell.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/18.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TFVoteSelectCell: UITableViewCell {
    @objc public var cellwidth:CGFloat = 0.0
    @objc public var tf_height:CGFloat = 0.0
    @objc public var view:UIView!
    
    @objc public var percentage:UIView!     //百分比颜色
    @objc public var opLabel:UILabel!
    @objc public var quantity:UILabel!   //详情

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.view = UIView.init()
        self.view.backgroundColor = UIColor(red: 244/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1.0)
        self.view.layer.cornerRadius = 4;
        self.view.layer.masksToBounds = true;
        self.contentView.addSubview(self.view)
   
        self.percentage = UIView.init()
        self.percentage.isHidden = true
        self.view.addSubview(self.percentage)
        self.percentage.backgroundColor = UIColor(red: 227/255.0, green: 234/255.0, blue: 241/255.0, alpha: 1.0)
        
        //名字
        self.opLabel = UILabel.init()
        self.view.addSubview(self.opLabel)
        self.opLabel.font = UIFont.PingFang(size: 16)
        self.opLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
        self.opLabel.numberOfLines = 0;
        self.opLabel.lineBreakMode = .byCharWrapping
        //百分比
        self.quantity = UILabel.init()
        self.quantity.textAlignment = .right
        self.quantity.numberOfLines = 0;
        self.quantity.lineBreakMode =  .byCharWrapping
        self.view.addSubview(self.quantity)
        self.quantity.font = UIFont.PingFang(size: 13)
        self.quantity.text = "275票  46%";
        self.quantity.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
                            
    }
    
    @objc public func cellConfig(_ model:TFVoteData, indexpath:NSIndexPath) {
        let arr = model.opList
  
        let op:TFOp = arr[indexpath.row] as! TFOp
        self.quantity.text = "\(op.opNum)" + "票 " + "\(op.percent)" + "%"
        self.opLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
        let opstr = "\(TalkfunSharedTools.asciiBytesToString(inx: indexpath.row))." + "\(op.op)"
        
        self.opLabel.text = opstr
        var endtimeF:CGFloat = 31
        
        if (model.info.endTime != "0") {
            endtimeF = 91+17;
        }
        
        let rect = TalkfunSharedTools.getRectWith(opstr, size: CGSize(width: self.cellwidth-48-endtimeF, height: CGFloat(MAXFLOAT)), fontSize: 16)
     
        self.view.frame = CGRect(x: 24, y: 0, width: self.cellwidth-48, height: rect.size.height+20)
 
        self.opLabel.frame = CGRect(x: 17, y: 0, width: self.view.frame.size.width-endtimeF, height: self.view.frame.size.height)
     
        self.quantity.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width - 17, height: self.view.frame.size.height)
      
        
        
        self.percentage.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width*(self.StringToFloat(str: op.percent)/100.0), height: self.view.frame.size.height)
      
        
        if (model.info.endTime != "0"||model.voted) {
            self.isUserInteractionEnabled = false;
        } else {
            self.isUserInteractionEnabled = true;
        }
        
        if (model.isShow&&model.info.endTime != "0") {
         
            self.view.backgroundColor = UIColor(red: 244/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1.0)
            
            self.view.layer.borderWidth = 0;
            self.view.layer.cornerRadius = 4;
            self.opLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
            self.percentage.backgroundColor = UIColor(red: 227/255.0, green: 234/255.0, blue: 241/255.0, alpha: 1.0)
            if model.userList.count > 0 {
          
                self.percentage.isHidden = false
                var userModel = TFVoteNewUser.init()
    
                for index in 0 ..< model.userList.count {
                    let dict:TFVoteNewUser = model.userList[index] as! TFVoteNewUser
                    if dict.isMe {
                        userModel = dict
                    }
                }
               
                let infoModel:TFVoteInfo = (model.info)
             
                
                //正确答案
                for inx in infoModel.answer {
                    if (Int(inx as! String))! == indexpath.row {
          
                        self.view.backgroundColor = UIColor(red: 224/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
                        self.view.layer.borderWidth = 0;
                        self.view.layer.cornerRadius = 4;
                        self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                        self.percentage.backgroundColor = UIColor(red: 199/255.0, green: 228/255.0, blue: 255/255.0, alpha: 1.0)
                       
                        
                    }
                }
                
                
                //选中的
            
                if (model.voted) {
                    for inx in userModel.option {
                        if (Int(inx as! String))! == indexpath.row {
                            
                            if (infoModel.answer.count == 0) {
                            self.view.backgroundColor = UIColor(red: 224/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0)
                                self.view.layer.borderWidth = 0;
                                self.view.layer.cornerRadius = 4;
                                self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)

                                self.percentage.backgroundColor = UIColor(red: 199/255.0, green: 228/255.0, blue: 255/255.0, alpha: 1.0)
                                self.opLabel.text = self.opLabel.text! + "\("已选")"
                            } else {
                                

                                self.view.backgroundColor = UIColor(red: 255/255.0, green: 240/255.0, blue: 236/255.0, alpha: 1.0)
                                    self.view.layer.borderWidth = 0;
                                self.view.layer.cornerRadius = 4;
                                self.opLabel.textColor = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1.0)
            
                            self.percentage.backgroundColor = UIColor(red: 255/255.0, green: 216/255.0, blue: 207/255.0, alpha: 1.0)
                              
                                self.opLabel.text = self.opLabel.text! + "\("已选")"
                      

                            }
                        }

                    }
                }
                //取出用户选择的答案与正确的答案一致
                let dict = NSMutableDictionary.init()
                
                if (infoModel.answer.count > userModel.option.count||infoModel.answer.count == userModel.option.count) {
                    for i in 0..<infoModel.answer.count {
                        let str:NSString = infoModel.answer[i] as! NSString
                        dict.setObject(str, forKey: str)
                    }
                    for i in 0..<userModel.option.count {
                        let str1:NSString = userModel.option[i] as! NSString
                        let valuess = dict.object(forKey: str1) as Any
                        
                        if valuess is String {
                          let value = valuess as? NSString
                            if value?.integerValue == indexpath.row {
                                self.view.backgroundColor = UIColor(red: 224/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0);
                                self.view.layer.borderWidth = 0;
                                self.view.layer.cornerRadius = 4;
                                self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                                self.percentage.backgroundColor = UIColor(red: 199/255.0, green: 228/255.0, blue: 255/255.0, alpha: 1.0)
                            }
                        }
                        
                     
                     
                    }
                     
                } else {
                    for i in 0..<userModel.option.count {
                        let str:NSString = userModel.option[i] as! NSString
                        
                        dict.setObject(str, forKey: str)
                    }
                    for i in 0..<infoModel.answer.count {
                        let valuess = dict.object(forKey: infoModel.answer[i]) as Any
                        if valuess is String {
                            let value = valuess as? NSString
                            if (value?.integerValue == indexpath.row) {
                                self.view.backgroundColor = UIColor(red: 224/255.0, green: 240/255.0, blue: 255/255.0, alpha: 1.0);
                                self.view.layer.borderWidth = 0;
                                self.view.layer.cornerRadius = 4;
                                self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                                self.percentage.backgroundColor = UIColor(red: 199/255.0, green: 228/255.0, blue: 255/255.0, alpha: 1.0)
                            }
                        }
                       
                    }
                }
                
                
            }

        } else {
           
         
            self.view.backgroundColor = UIColor(red: 244/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1.0)
            self.view.layer.borderWidth = 0;
            self.view.layer.borderColor = UIColor.clear.cgColor
            self.opLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)

                if (op.selected) {
                     self.view.backgroundColor = UIColor.white
                     self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                     self.view.layer.borderWidth = 1.0;
                    self.view.layer.borderColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0).cgColor
                 }


            //学员选择的答案
            if model.userList.count > 0 {
                var userModel1 = TFVoteNewUser.init()
                for index in 0 ..< model.userList.count {
                    let dict:TFVoteNewUser = model.userList[index] as! TFVoteNewUser
                    if dict.isMe {
                        userModel1 = dict
                    }
                }
              
                for inx in userModel1.option {
                    if (Int(inx as! String))! == indexpath.row {
                        if userModel1.isMe {
                            self.view.backgroundColor = UIColor.white

                            self.view.layer.borderColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0).cgColor
                            self.view.layer.borderWidth = 1;
                            self.view.layer.cornerRadius = 4;

                            self.opLabel.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
                        }
                   
                    }
                }

            }

        }
        
        
        
        //公布答案
        if (model.isShow) {
            
            self.quantity.isHidden = false;
            self.percentage.isHidden = false;
        }else{
            self.quantity.isHidden = true;
            //百分比
            self.percentage.isHidden = true;
        }
        
    }
    
    func StringToFloat(str:String)->(CGFloat){


           let string = str

           var cgFloat:CGFloat = 0

           if let doubleValue = Double(string)

           {

               cgFloat = CGFloat(doubleValue)

           }

           return cgFloat

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
